package org.wanho.servlet;

import com.alibaba.fastjson.JSON;
import org.wanho.entity.User;
import org.wanho.service.UserService;
import org.wanho.service.impl.UserServiceImpl;
import org.wanho.vo.AjaxResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * Author：汤小洋
 * Date：2024-12-03 19:04
 * Description：<描述>
 */
@WebServlet("/list")
public class FindUserServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<User> list = userService.findAll();

        // 允许跨域访问
        resp.setHeader("Access-Control-Allow-Origin", "*"); // 允许任何域的跨域请求

        // 响应结果
        resp.setContentType("application/json;charset=utf8");
        PrintWriter out = resp.getWriter();

        out.print(JSON.toJSONString(AjaxResult.success("查询成功", list)));
        out.close();
    }
}
